package com.cmccsi.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

public class RedisProperties {

	private static final Logger logger = Logger.getLogger(RedisProperties.class);
	private static final Properties p = new Properties();
	private static ResourceBundle resource;
	
	public static String host;
	public static String port;
	public static int expire;
	public static int expireForWeb;
	public static int minIdle;
	public static int maxIdle;
	public static int maxTotal;
	public static int maxWait;
	public static boolean testOnBorrow;
	public static boolean testOnReturn;
	public static boolean blockWhenExhausted;

	static {
		InputStream in = null;
		try {
			resource = ResourceBundle.getBundle("redis");			
			host = getString("host");
			port = getString("port");
			expire = getInt("expire");
			expireForWeb = getInt("expireForWeb");
			minIdle = getInt("minIdle");
			maxIdle = getInt("maxIdle");
			maxTotal = getInt("maxTotal");
			maxWait = getInt("maxWait");
			testOnBorrow = getBoolean("testOnBorrow");
			testOnReturn = getBoolean("testOnReturn");
			blockWhenExhausted = getBoolean("blockWhenExhausted");
		} catch (Exception e) {
			logger.error("读取redis.properties异常", e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static String getString(String key) {
		try {
			return resource.getString(key);
		} catch (Exception e) {
			return null;
		}
	}
	
	public static int getInt(String key) {
		try {
			return Integer.parseInt(resource.getString(key));
		} catch (Exception e) {
			return 0;
		}
	}
	
	public static boolean getBoolean(String key) {
		try {
			return Boolean.parseBoolean(resource.getString(key));
		} catch (Exception e) {
			return false;
		}
	}

}
